class Create<%= namespacing %><%= class_name.pluralize %> < ActiveRecord::Migration[<%= "#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}" %>]

  def up
    create_table :refinery_<%= "#{namespacing.underscore}_" if table_name != namespacing.underscore.pluralize -%><%= table_name %> do |t|
<% (attributes - localized_attributes).each do |attribute| -%>
      t.<%= attribute.type %> :<%= attribute.column_name %>
<% end -%>
      t.integer :position

      t.timestamps
    end

<% if localized? %>
    create_table :<%= localized_table_name %> do |t|
<% localized_attributes.each do |attribute| -%>
      t.<%= attribute.type %> :<%= attribute.column_name %>
<% end -%>
      t.string  :locale, null: false
      t.integer :refinery_<%= singular_table_name %>_id, null: false
      t.timestamps
    end

    add_index :<%= localized_table_name %>, :locale, name: :index_<%= localized_table_name %>_on_locale
    add_index :<%= localized_table_name %>, [:refinery_<%= singular_table_name %>_id, :locale], name: :index_<%= Digest::SHA1.hexdigest(localized_table_name) %>, unique: true
<% end %>
  end


  def down
    if defined?(::Refinery::UserPlugin)
      ::Refinery::UserPlugin.destroy_all({:name => "refinerycms-<%= namespacing.underscore %>"})
    end
<% unless skip_frontend? %>
    if defined?(::Refinery::Page)
      ::Refinery::Page.delete_all({:link_url => "/<%= namespacing.underscore %>/<%= plural_name %>"})
    end
<% end %>
    drop_table :refinery_<%= "#{namespacing.underscore}_" if table_name != namespacing.underscore.pluralize -%><%= table_name %>
<% if localized? %>
    drop_table :<%= localized_table_name %>
<% end %>
  end
end
